using System;
using UnityEngine;

namespace EventBehaviorTree
{
    [Serializable]
    public class RootNode : Container
    {
        public RootNode() { }
        public RootNode(string key, MonoBehaviour coroutineStarter)
        {
            SetKey(key);
            SetCoroutineStarter(coroutineStarter);
        }
        public override void NodeEvent(NodeMsg nodeMessage)
        {
            switch (nodeMessage)
            {
                case NodeMsg.Start:
                    if (!_isWorking)
                    {
                        _isWorking = true;
                        _stepCount = 0;
                        _subNodes[0].NodeEvent(nodeMessage);
                    }
                    break;
                case NodeMsg.Succeed:
                    if (_stepCount < _subNodes.Count - 1)
                    {
                        _subNodes[++_stepCount].NodeEvent(NodeMsg.Start);//start all sub nodes
                    }
                    break;//_isWorking will always be true
                case NodeMsg.Fail:
                case NodeMsg.REQ_Abort://abort request from subnode
                    StopAllSubNodes();
                    _isWorking = false;
                    break;
                case NodeMsg.CMD_Stop://system stop
                    if (_isWorking)
                    {
                        StopAllSubNodes();
                        _isWorking = false;
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
